//index.js
//获取应用实例
const app = getApp().globalData;
var This;

Page({
  data: {
    page: 1,
    banpost: 0,
    sortid: 0,
    sorttitle: '最新动态',
    listdata: false
  },
  //页面加载
  onLoad: function (options) {
    This = this;
    wx.showLoading({
      title: '正在初始化',
      mask: true
    });
    //读取参数
    setTimeout(function () {
      This.setData({
        sortid: options.sortid || 0,
        banpost: parseInt(wx.getStorageSync('HS_BANPOST')) || 0
      });
    }, (app._G.WX_HS_STATE ? 0 : 1500));
    //列表加载
    This.LoadList(1);
  },
  //页面加载完成
  onReady: function () {
    //获取列表分类id
    setInterval(function () {
      var _sortid = parseInt(wx.getStorageSync('_SORTID')) || 0;
      var _sorttitle = wx.getStorageSync('_SORTTITLE') || '最新动态';
      //console.log(_sortid + ' ' + _sorttitle);
      if (_sortid != This.data.sortid) {
        wx.showLoading({
          title: '正在加载',
          mask: true
        });
        This.setData({
          sortid: _sortid,
          sorttitle: _sorttitle
        });
        wx.setNavigationBarTitle({
          title: This.data.sorttitle
        });
        This.LoadList(1);
      }
    }, 1000);
  },
  //下拉刷新
  onPullDownRefresh: function () {
    this.LoadList(1);
    setTimeout(function () {
      wx.stopPullDownRefresh()
    }, 1500);
  },
  //上拉触底
  onReachBottom: function () {
    this.setData({
      page: this.data.page + 1
    });
    this.LoadList(this.data.page);
  },
  LoadList: function (page) {
	  
    wx.request({
      url: app.BBS + 'index.php?c=app&a=puyuetian_wxxcx&s=list',
      data: {
        "sortid": This.data.sortid,
        "hs_chkcode": app._G.HS_CHKCODE,
        "page": page,
        "rnd": Math.random()
      }, header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      dataType: 'json',
      success: res => {
        if (res.data['state'] == 'ok') {
          if (this.data.page > 1) {
            var d = this.data.listdata
            var l = d.length
            for (var i = 0; i < res.data['datas'].length; i++) {
              d[i + l] = res.data['datas'][i]
            }
            res.data['datas'] = d
          }
          this.setData({
            listdata: res.data['datas']
          })
          console.log(res.data['datas'])
        } else {
          wx.showToast({
            title: '出错'
          })
        }
        wx.hideLoading();
      },
      fail: res => {
        console.log(res);
        wx.showToast({
          title: '获取数据失败'
        });
        wx.hideLoading();
      }
    })
  },
  LookRead: event => {
    var readid = event.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/read/index?id=' + readid
    });
  },
  //发布新文章
  PostRead: event => {
    wx.navigateTo({
      url: '/pages/edit/index?readid=0&type=read'
    });
  },
  //回首页
  backHome: function () {
    wx.showLoading({
      title: '正在加载',
      mask: true
    });
    wx.setStorageSync('_SORTID', '0');
    wx.setStorageSync('_SORTTITLE', '');
  }
})
